package com.xhwl.centiotdata.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.handler.security.SecurityIntegrationFactory;
import com.xhwl.centiotdata.pojo.dto.huawei.IOTConfigDTO;
import com.xhwl.centiotdata.service.ISecurityPatrolService;
import com.xhwl.centiotdata.service.integrateconfig.SecurityIntegrationConfig;
import com.xhwl.centiotdata.utils.ThreadPoolUtils;
import com.xhwl.common.pojo.cent.device.DeviceProjSupplierCategory;
import com.xhwl.common.pojo.cent.security.PatrollingLine;
import com.xhwl.common.pojo.cent.security.PatrollingPlan;
import com.xhwl.common.utils.ChkUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Falcon.cao @Copyright (C), 2022, 兴海物联
 * Date: 2022/8/9 10:27
 * Description: 包信息
 */
@Slf4j
@Service
public class SecurityPatrolServiceImpl implements ISecurityPatrolService {

    @Autowired
    private SecurityIntegrationConfig securityIntegrationConfig;

    @Override
    public String onAddingPatrolsLine(PatrollingLine line, List<String> nodeIds) {
        List<DeviceProjSupplierCategory> configs = securityIntegrationConfig.getIntegrationConfig(line.getProjectId());
        if (ChkUtil.isNull(configs)) {
            return null;
        }
        ThreadPoolUtils.executor.execute(() -> {
            for (DeviceProjSupplierCategory config : configs) {
                JSONObject properties = config.getProperties();
                if (ChkUtil.isNull(properties)) {
                    continue;
                }
                IOTConfigDTO configDTO = properties.toJavaObject(IOTConfigDTO.class);
                JSONObject result = SecurityIntegrationFactory.getInvokeStrategy(config.getProductId()).onPushingPatrolsLine(
                        configDTO, line, null, nodeIds, configDTO.getPatrolineCC()
                );
                log.info("添加巡更线路结果：{}", result.toJSONString());
            }
        });
        return line.getName();
    }

    @Override
    public String onUpdatePatrolsLine(PatrollingLine line, String oldName, List<String> nodeIds) {
        List<DeviceProjSupplierCategory> configs = securityIntegrationConfig.getIntegrationConfig(line.getProjectId());
        if (ChkUtil.isNull(configs)) {
            return null;
        }
        ThreadPoolUtils.executor.execute(() -> {
            for (DeviceProjSupplierCategory config : configs) {
                JSONObject properties = config.getProperties();
                if (ChkUtil.isNull(properties)) {
                    continue;
                }
                IOTConfigDTO configDTO = properties.toJavaObject(IOTConfigDTO.class);
                JSONObject result = SecurityIntegrationFactory.getInvokeStrategy(config.getProductId()).onPushingPatrolsLine(
                        configDTO, line, oldName, nodeIds, configDTO.getPatrolineUC()
                );
                log.info("更新巡更线路结果：{}", result.toJSONString());
            }
        });
        return line.getName();
    }

    @Override
    public String onDeletePatrolsLine(Integer projectId, String platformId) {
        List<DeviceProjSupplierCategory> configs = securityIntegrationConfig.getIntegrationConfig(projectId);
        if (ChkUtil.isNull(configs)) {
            return null;
        }
        ThreadPoolUtils.executor.execute(() -> {
            for (DeviceProjSupplierCategory config : configs) {
                JSONObject properties = config.getProperties();
                if (ChkUtil.isNull(properties)) {
                    continue;
                }
                PatrollingLine line = new PatrollingLine();
                line.setPlatformId(platformId);
                line.setName(platformId);
                IOTConfigDTO configDTO = properties.toJavaObject(IOTConfigDTO.class);
                JSONObject result = SecurityIntegrationFactory.getInvokeStrategy(config.getProductId()).onPushingPatrolsLine(
                        configDTO, line, null, null, configDTO.getPatrolineDC()
                );
                log.info("删除巡更线路结果：{}", result.toJSONString());
            }
        });
        return platformId;
    }

    @Override
    public String onAddingPatrolsPlan(JSONObject map) {
        Integer projectId = map.getInteger("projectId");
        if (projectId == null) {
            return null;
        }
        ThreadPoolUtils.executor.execute(() -> {
            List<DeviceProjSupplierCategory> configs = securityIntegrationConfig.getIntegrationConfig(projectId);
            if (ChkUtil.isNull(configs)) {
                return;
            }
            for (DeviceProjSupplierCategory config : configs) {
                JSONObject properties = config.getProperties();
                if (ChkUtil.isNull(properties)) {
                    continue;
                }
                IOTConfigDTO configDTO = properties.toJavaObject(IOTConfigDTO.class);

                PatrollingPlan plan = map.getJSONObject("plan").toJavaObject(PatrollingPlan.class);
                JSONObject result = SecurityIntegrationFactory.getInvokeStrategy(config.getProductId()).onPushingPatrolsPlan(
                        configDTO, plan, configDTO.getPatroplanCC()
                );
                log.info("添加巡更计划结果：{}", result.toJSONString());
            }
        });
        return null;
    }

    @Override
    public String onUpdatePatrolsPlan(JSONObject map) {
        Integer projectId = map.getInteger("projectId");
        if (projectId == null) {
            return null;
        }
        List<DeviceProjSupplierCategory> configs = securityIntegrationConfig.getIntegrationConfig(projectId);
        if (ChkUtil.isNull(configs)) {
            return null;
        }
        ThreadPoolUtils.executor.execute(() -> {
            for (DeviceProjSupplierCategory config : configs) {
                JSONObject properties = config.getProperties();
                if (ChkUtil.isNull(properties)) {
                    continue;
                }
                IOTConfigDTO configDTO = properties.toJavaObject(IOTConfigDTO.class);

                PatrollingPlan plan = map.getJSONObject("plan").toJavaObject(PatrollingPlan.class);
                JSONObject result = SecurityIntegrationFactory.getInvokeStrategy(config.getProductId()).onPushingPatrolsPlan(
                        configDTO, plan, configDTO.getPatroplanUC()
                );
                log.info("修改巡更计划结果：{}", result.toJSONString());
            }
        });
        return null;
    }

    @Override
    public String onDeletePatrolsPlan(Integer projectId, String platformId) {
        List<DeviceProjSupplierCategory> configs = securityIntegrationConfig.getIntegrationConfig(projectId);
        if (ChkUtil.isNull(configs)) {
            return null;
        }
        ThreadPoolUtils.executor.execute(() -> {
            for (DeviceProjSupplierCategory config : configs) {
                JSONObject properties = config.getProperties();
                if (ChkUtil.isNull(properties)) {
                    continue;
                }
                IOTConfigDTO configDTO = properties.toJavaObject(IOTConfigDTO.class);

                PatrollingPlan plan = new PatrollingPlan();
                plan.setPlatformId(platformId);

                JSONObject result = SecurityIntegrationFactory.getInvokeStrategy(config.getProductId()).onPushingPatrolsPlan(
                        configDTO, plan, configDTO.getPatroplanDC()
                );
                log.info("添加巡更计划结果：{}", result.toJSONString());
            }
        });
        return null;
    }

    @Override
    public String onGetterCheckRecord(JSONObject map) {
        List<DeviceProjSupplierCategory> configs = securityIntegrationConfig.getIntegrationConfig(map.getInteger("projectId"));
        if (ChkUtil.isNull(configs)) {
            return null;
        }
        ThreadPoolUtils.executor.execute(() -> {
            for (DeviceProjSupplierCategory config : configs) {
                JSONObject properties = config.getProperties();
                if (ChkUtil.isNull(properties)) {
                    continue;
                }
                IOTConfigDTO configDTO = properties.toJavaObject(IOTConfigDTO.class);

                JSONObject result = SecurityIntegrationFactory.getInvokeStrategy(config.getProductId()).onGettingCheckRecord(
                        configDTO, map, configDTO.getCheckRecord()
                );
                log.info("添加巡更计划结果：{}", result.toJSONString());
            }
        });
        return null;
    }
}
